package com.zhoug.common3.fileselector;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.zhoug.common3.R;
import com.zhoug.common3.adapters.recycler2.BaseRecyclerViewAdapter;
import com.zhoug.common3.adapters.recycler2.BaseViewHolder;
import com.zhoug.common3.anim.ZAnimations;
import com.zhoug.common3.content.FileType;
import com.zhoug.common3.imageloader.ZImageLoader;
import com.zhoug.common3.utils.IntentUtils;
import com.zhoug.common3.utils.file.IFile;
import com.zhoug.common3.view.dialog.OnItemClickListener;
import com.zhoug.common3.view.dialog.ZListDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 文件展示控件(默认网格展示4列)
 *
 * @Author HK-LJJ
 * @Date 2020/1/3
 * @Description
 */
public class FilePreview extends FrameLayout {
    private static final String TAG = ">>>FilePreview";
    private RecyclerView recyclerView;
    private Adapter mAdapter;
    private boolean deleteEnable = false;//是否可以删除
    private final ArrayList<IFile> files = new ArrayList<>();
    private OnDeleteListener onDeleteListener;
    private BaseRecyclerViewAdapter.OnItemClickListener<IFile> mOnItemClickListener;

    public FilePreview(@NonNull Context context) {
        this(context, null);
    }

    public FilePreview(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FilePreview(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.common_file_preview, null);
        recyclerView = view.findViewById(R.id.recyclerView_file);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        mAdapter = new Adapter();
        mAdapter.setData(files);
        mAdapter.setOnItemClickListener((adapter, view1, position) -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(adapter, view1, position);
            } else {
                defOnItemClickListener.onItemClick(adapter, view1, position);
            }
        });
        recyclerView.setAdapter(mAdapter);
        addView(view);
    }

    /**
     * 默认的图片单击事件
     */
    private BaseRecyclerViewAdapter.OnItemClickListener<IFile> defOnItemClickListener = new BaseRecyclerViewAdapter.OnItemClickListener<IFile>() {
        @Override
        public void onItemClick(BaseRecyclerViewAdapter<IFile> adapter, View view, int position) {
            IFile comFile = mAdapter.getItemData(position);
            if (deleteEnable) {
                ZListDialog<String> dialog = new ZListDialog<String>(getContext()) {

                    @Override
                    public String getText(String itemData) {
                        return itemData;
                    }
                };
                dialog.setCanceledOnTouchOutside(true);
                dialog.setFullWidth(true);
                dialog.setWindowAnimations(ZAnimations.ANIM_DOWN);
                dialog.setAttributes(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
                dialog.setTitle(null);
                dialog.setData(Arrays.asList("查看", "删除"));
                dialog.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int pos) {
                        dialog.cancel();
                        if (pos == 0) {
                            //查看
//                        toast("查看");
                            toLook(position);
                        } else if (pos == 1) {
                            //删除
                            files.remove(comFile);
                            mAdapter.notifyDataSetChanged();
                            if (onDeleteListener != null) {
                                onDeleteListener.onDelete(comFile);
                            }
//                        toast("删除");
                        }
                    }
                });
                dialog.show();
            } else {
                //查看
//                toast("查看");
                toLook(position);
            }


        }
    };

    /**
     * 适配器
     */
    public class Adapter extends BaseRecyclerViewAdapter<IFile> {
        private static final int VIEW_TYPE_IMAGE = 101;
        private static final int VIEW_TYPE_OTHER = 102;

        @Override
        public int getItemViewType(int position) {
            int itemViewType = super.getItemViewType(position);
            if (itemViewType == VIEW_TYPE_DATA) {
                IFile itemData = getItemData(position);
                if (itemData != null) {
                    int fileType = itemData.getFileType();
                    if (fileType == FileType.IMAGE || fileType == FileType.VIDEO) {
                        return VIEW_TYPE_IMAGE;
                    } else {
                        return VIEW_TYPE_OTHER;
                    }
                }
            }
            return itemViewType;
        }

        @Override
        public int getLayoutId(int viewType) {
            if (viewType == VIEW_TYPE_IMAGE) {
                return R.layout.common_file_preview_item;
            } else {
                return R.layout.common_file_preview_item_other;
            }
        }

        @Override
        public void onBindData(BaseViewHolder holder, IFile data, int position, int viewType) {
            if (viewType == VIEW_TYPE_IMAGE) {
                ImageView imageView = holder.getView(R.id.img_file_item);
                ImageView ivVideo = holder.getView(R.id.iv_video);
                ImageView ivAudio = holder.getView(R.id.iv_audio);
                if (data != null) {
//                LogUtils.d(TAG, "onBindData: data=" + data);
                    switch (data.getFileType()) {
                        case FileType.IMAGE:
                            ZImageLoader.load(FilePreview.this.getContext(), imageView, data.getRealUrl());
                            imageView.setVisibility(View.VISIBLE);
                            ivVideo.setVisibility(View.GONE);
                            ivAudio.setVisibility(View.GONE);
                            break;
                        case FileType.VIDEO:
                            ZImageLoader.load(FilePreview.this.getContext(), imageView, data.getRealUrl());
                            imageView.setVisibility(View.VISIBLE);
                            ivVideo.setVisibility(View.VISIBLE);
                            ivAudio.setVisibility(View.GONE);

                            break;
                        case FileType.AUDIO:
                            imageView.setVisibility(View.GONE);
                            ivVideo.setVisibility(View.GONE);
                            ivAudio.setVisibility(View.VISIBLE);

                            break;
                    }

                }
            } else {
                ImageView imageView = holder.getView(R.id.img_file_item);
                if (data != null) {
                    switch (data.getFileType()) {
                        case FileType.AUDIO:
                            imageView.setImageResource(R.drawable.fileselector_icon_audio2);
                            break;
                        case FileType.WORD:
                            imageView.setImageResource(R.drawable.fileselector_icon_word);
                            break;
                        case FileType.EXCEL:
                            imageView.setImageResource(R.drawable.fileselector_icon_xls);
                            break;
                        case FileType.PDF:
                            imageView.setImageResource(R.drawable.fileselector_icon_pdf);
                            break;
                        case FileType.TXT:
                            imageView.setImageResource(R.drawable.fileselector_icon_txt);
                            break;
                    }
                }

            }

        }

    }


    private void toLook(int position) {
        IFile iFile = files.get(position);
        String realUrl = iFile.getRealUrl();
        Intent intent = IntentUtils.getReadFileIntent(getContext(), realUrl, FileType.getMimeType(realUrl));
        getContext().startActivity(intent);

    }

    public boolean isDeleteEnable() {
        return deleteEnable;
    }

    public void setDeleteEnable(boolean deleteEnable) {
        this.deleteEnable = deleteEnable;
    }

    public <T extends IFile> void addFile(T file) {
        if (file != null) {
            files.add(file);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Deprecated
    public <T extends IFile> void addAllFile(List<T> files) {
        addFiles(files);
    }

    public <T extends IFile> void addFiles(List<T> comFiles) {
        if (comFiles != null && !comFiles.isEmpty()) {
            files.addAll(comFiles);
            mAdapter.notifyDataSetChanged();
        }
    }

    public <T extends IFile> void setFiles(List<T> files) {
        this.files.clear();
        addFiles(files);
    }

    public <T extends IFile> void setFile(T file) {
        this.files.clear();
        addFile(file);
    }

    public ArrayList<IFile> getFiles() {
        return files;
    }

    public ArrayList<IFile> getCopyFiles() {
        return files.isEmpty() ? null : new ArrayList<>(files);
    }


    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public Adapter getAdapter() {
        return mAdapter;
    }

    public OnDeleteListener getOnDeleteListener() {
        return onDeleteListener;
    }

    public void setOnItemClickListener(BaseRecyclerViewAdapter.OnItemClickListener<IFile> onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setOnDeleteListener(OnDeleteListener onDeleteListener) {
        this.onDeleteListener = onDeleteListener;
    }

    public interface OnDeleteListener {
        void onDelete(IFile iFile);
    }


}
